package com.yeebo.common;

import com.yeebo.constants.Constants;
import com.yeebo.interceptor.Access;
import com.yeebo.model.user.User;
import com.yeebo.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 公共接口层
 */
@Api("通用接口")
@RestController
public class CommonController extends BaseController {

    @Resource
    private RedisUtils redisUtils;
    @Autowired
    private AmqpTemplate amqpTemplate;

    @RequestMapping("/")
    @ResponseBody
    public String welcome() {
        return "Hello Api!";
    }

    @ApiOperation(value = "测试Redis锁", notes = "测试并发锁")
    @RequestMapping("/testRedisLock")
    @ResponseBody
    public String testRedisLock() {
        redisUtils.lock("hello", 30000L);
        System.out.println("正在处理中……");
        redisUtils.releaseLock("hello");
        return "Lock Success";
    }

    @RequestMapping("/testRabbitSender")
    @ResponseBody
    public String testRabbitSender() {
        User user = new User();
        user.setId(1L);
        user.setName("admin");
        amqpTemplate.convertAndSend(Constants.TEST_QUEUE_KEY, user);
        System.out.println("发送消息成功");
        return "Send Success";
    }

    @Access(needToken = true)
    @RequestMapping("/testNeedToken")
    @ResponseBody
    public String testNeedToken() {
        return "Token Valid";
    }

}
